package com.dubbo.server.service;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dubbo.server.mapper.PublishInfoMapper;
import com.dubbo.server.mapper.UserInfoMapper;
import com.dubbo.server.pojo.PublishInfo;
import com.dubbo.server.pojo.UserInfo;
import com.dubbo.server.vo.MessageDetailVo;
import com.tanhua.common.pojo.Publish;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class MessageCheckService {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Autowired
    private PublishInfoMapper publishInfoMapper;


    private String checkMsg = "";

    //这个是用来查询sql的userInfo
    @Autowired
    private UserInfoMapper userInfoMapper;


    /**
     * 消息置顶
     *
     * @param id
     * @return
     */
    public String setTopState(String id) {
        PublishInfo publishInfo = this.queryPublishInfoById(Integer.valueOf(id));
        //如果搜索为空，没有找到动态信息，就返回null
        if (ObjectUtil.isEmpty(publishInfo)) {
            return null;
        }
        Boolean topState = publishInfo.getTopState();
        //判断topState是否为1，1代表未置顶，如果未置顶就弄成2置顶
        if (!topState) {
            topState = true;
            publishInfo.setTopState(topState);
        } else if (topState) { //取消置顶
            topState = false;
            publishInfo.setTopState(topState);
        }
        //修改更新时间
        publishInfo.setUpdated(new Date());


        QueryWrapper<PublishInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", id);
        this.publishInfoMapper.update(publishInfo, queryWrapper);


        return "操作成功";
    }

    /**
     * 查询消息详情
     *
     * @param id
     * @return
     */
    //把这三个东西进行合并然后组合成Vo返回给controller    618f99a63b8fe20274160560
    public MessageDetailVo queryMessageDetail(String id) {
        //messageDetailVo对象，把内容拼凑回这个对象
        MessageDetailVo messageDetailVo = new MessageDetailVo();
        //1.通过publish_info去找要的参数
        PublishInfo publishInfo = this.queryPublishInfoById(id);
        messageDetailVo.setId(id); //编号
        messageDetailVo.setUserId(Math.toIntExact(publishInfo.getUserId()));   //作者id
        messageDetailVo.setTopState(publishInfo.getTopState() ? 2 : 1);  //置顶状态
        messageDetailVo.setCreateDate(publishInfo.getCreated());   //发布时间
        messageDetailVo.setState(Integer.valueOf(publishInfo.getState()));//审核状态，1为待审核，2为自动审核通过，3为待人工审核，4为人工审核拒绝，5为人工审核通过，6为自动审核拒绝
        messageDetailVo.setCommentCount(publishInfo.getCommentCount());//评论数
        messageDetailVo.setLikeCount(publishInfo.getLikeCount());//点赞数
        messageDetailVo.setReportCount(publishInfo.getReportCount());//举报数
        messageDetailVo.setForwardingCount(publishInfo.getForwardingCount());//转发数


        if (ObjectUtil.isEmpty(publishInfo)) {
            return messageDetailVo;
        }

        //通过publishInfo去获取userId
        String publishId = publishInfo.getPublishId();
        Publish publish = queryPublishByPublishId(publishId);
        if (ObjectUtil.isEmpty(publish)) {
            return messageDetailVo;
        }

        //取出text，medias给messageDetailVo返回对象
        messageDetailVo.setText(publish.getText());   //圈子内容
        messageDetailVo.setMedias(Convert.toStrArray(publish.getMedias()));
        //3.通过quznzi_publish找出userId去查userInfo
        Long userId = publish.getUserId();
        UserInfo userInfo = this.queryUserInfoByUserId(userId);
        //将nickname和userLogo给medias给messageDetailVo返回对象
        messageDetailVo.setNickname(userInfo.getNickName());//昵称
        messageDetailVo.setUserLogo(userInfo.getLogo());//头像
        if (ObjectUtil.isEmpty(userInfo)) {
            return messageDetailVo;
        }
        return messageDetailVo;

    }


    public Publish queryPublishByPublishId(String id) {

        return this.mongoTemplate.findById(id, Publish.class);
    }

    //这个方法查询publishInfo的信息，然后从中提取我要的几个参数用于vo拼凑
    private PublishInfo queryPublishInfoById(Object id) {
        QueryWrapper<PublishInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("publish_id", id);
        return this.publishInfoMapper.selectOne(queryWrapper);
    }


    ///这个方法查询userInfo的信息，然后从中提取我要的几个参数用于vo拼凑
    private UserInfo queryUserInfoByUserId(Long userId) {
        QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        return this.userInfoMapper.selectOne(queryWrapper);
    }

}